package demo1;

public class SingleList implements IList{

    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            return;
        }
        ListNode cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    private void checkIndexInAdd(int index) throws IndexException {
        if(index < 0 || index > size()) {
            throw new IndexException("下标范围不合法！");
        }
    }

    @Override
    public void addIndex(int index, int data) {
        try{
            checkIndexInAdd(index);
            if(index == 0) {
                addFirst(data);
                return;
            }
            //找到index前一个的节点
            ListNode cur = head;
            for (int i = 0; i < index - 1; i++) {
                cur = cur.next;
            }
            ListNode node = new ListNode(data);
            node.next = cur.next;
            cur.next = node;
        } catch (IndexException e) {
            System.out.println("index 不合法！");
            e.printStackTrace();
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    private ListNode findFrontNodeOfKey(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public void remove(int key) {

        //空链表
        if(head == null) {
            return;
        }

        //头删
        if(head.val == key) {
            head = head.next;
            return;
        }

        ListNode prev = findFrontNodeOfKey(key);
        if(prev == null) {
            return;//不存在key
        }

        ListNode del = prev.next;
        prev.next = del.next;
    }

    @Override
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }

        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
            } else {
                prev = cur;
            }
            cur = cur.next;
        }

        if(head.val == key) {
            head = head.next;
        }
    }

    @Override
    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while(cur != null) {
            ListNode tmp = cur.next;
            cur.next = null;
            cur = tmp;
        }
        head = null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}